package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.PerformanceService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.performance.pojo.dto.PerformanceAggDepartmentDTO;
import com.xbongbong.pro.performance.pojo.dto.PerformanceChartListDTO;
import com.xbongbong.pro.performance.pojo.dto.PerformanceConfigListDTO;
import com.xbongbong.pro.performance.pojo.dto.PerformanceDetailDTO;
import com.xbongbong.pro.performance.pojo.dto.PerformanceEditListDTO;
import com.xbongbong.pro.performance.pojo.dto.PerformanceItemDTO;
import com.xbongbong.pro.performance.pojo.dto.PerformanceListDTO;
import com.xbongbong.pro.performance.pojo.dto.PerformanceSaveWebDTO;
import com.xbongbong.pro.performance.pojo.dto.PerformanceSumDataDTO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceAggDepartmentVO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceChartListVO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceConfigListVO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceDetailVO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceEditListVO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceItemVO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceListVO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceSaveVO;
import com.xbongbong.pro.performance.pojo.vo.PerformanceSumDataVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description: 业绩目标
 * @Author: hongxiao
 * @date: 2019-01-22 14:36
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/performance")
public class PerformanceController {

    @Resource
    private PerformanceService performanceService;

    /**
     * 获取业绩目标配置
     * @param performanceConfigListDTO 入参
     * @return
     *
     * @author hongxiao
     * @addTime: 2019-01-24 14:44
     * @since
     *
     * @modified by:
     * @updateTime: 2019-01-24 14:44
     * @version
     */
    @RequestMapping(value = "/configList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String configList(@RequestBody @Valid PerformanceConfigListDTO performanceConfigListDTO, BindingResult br) {
        XbbResponse<PerformanceConfigListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceConfigListVO performanceConfigListVO = performanceService.configList(performanceConfigListDTO);
                response = new XbbResponse<>(performanceConfigListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 设置目标初始化
     * @param performanceItemDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-18 11:23
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/itemWeb", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String itemWeb(@RequestBody @Valid PerformanceItemDTO performanceItemDTO, BindingResult br) {
        XbbResponse<PerformanceItemVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceItemVO performanceItemVO = performanceService.item(performanceItemDTO);
                response = new XbbResponse<>(performanceItemVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 保存业绩目标
     * @param performanceSaveWebDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-18 11:37
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/saveWeb", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveWeb(@RequestBody @Valid PerformanceSaveWebDTO performanceSaveWebDTO, BindingResult br) {
        XbbResponse<PerformanceSaveVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSaveVO performanceSaveVO = performanceService.saveWeb(performanceSaveWebDTO);
                response = new XbbResponse<>(performanceSaveVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 列表页编辑单个目标
     * @param performanceEditListDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-18 11:44
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/editList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String editList(@RequestBody @Valid PerformanceEditListDTO performanceEditListDTO, BindingResult br) {
        XbbResponse<PerformanceEditListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceEditListVO performanceEditListVO = performanceService.editList(performanceEditListDTO);
                response = new XbbResponse<>(performanceEditListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 目标列表
     * @param performanceListDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-19 11:13
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid PerformanceListDTO performanceListDTO, BindingResult br) {
        XbbResponse<PerformanceListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceListVO performanceListVO = performanceService.list(performanceListDTO);
                response = new XbbResponse<>(performanceListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 目标列表_员工目标汇总
     * @param performanceSumDataDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-19 17:28
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/sumData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sumData(@RequestBody @Valid PerformanceSumDataDTO performanceSumDataDTO, BindingResult br) {
        XbbResponse<PerformanceSumDataVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceSumDataVO performanceSumDataVO = performanceService.sumData(performanceSumDataDTO);
                response = new XbbResponse<>(performanceSumDataVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 业绩目标详情
     * @param performanceDetailDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-19 20:51
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String detail(@RequestBody @Valid PerformanceDetailDTO performanceDetailDTO, BindingResult br) {
        XbbResponse<PerformanceDetailVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceDetailVO performanceSumDataVO = performanceService.detail(performanceDetailDTO);
                response = new XbbResponse<>(performanceSumDataVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 一键汇总
     * @param performanceAggDepartmentDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-19 21:31
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/aggDepartment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String aggDepartment(@RequestBody @Valid PerformanceAggDepartmentDTO performanceAggDepartmentDTO, BindingResult br) {
        XbbResponse<PerformanceAggDepartmentVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceAggDepartmentVO performanceAggDepartmentVO = performanceService.aggDepartment(performanceAggDepartmentDTO);
                response = new XbbResponse<>(performanceAggDepartmentVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 目标列表
     * @param performanceChartListDTO 入参
     * @param br
     * @return
     * @throws
     *
     * @author hongxiao
     * @date: 2019-02-19 11:13
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/chart/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String ChartList(@RequestBody @Valid PerformanceChartListDTO performanceChartListDTO, BindingResult br) {
        XbbResponse<PerformanceChartListVO> response;
        if(br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                PerformanceChartListVO performanceListVO = performanceService.chartList(performanceChartListDTO);
                response = new XbbResponse<>(performanceListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
